package com.ctshk.rpc.goods.service.impl;

import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.goods.dto.CommoditySpecificationsAttributeDTO;
import com.ctshk.rpc.goods.req.CommoditySpecificationsAttributeAddReq;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.rpc.goods.entity.CommoditySpecificationsAttribute;
import com.ctshk.rpc.goods.mapper.CommoditySpecificationsAttributeMapper;
import com.ctshk.rpc.goods.service.ICommoditySpecificationsAttributeService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品规格属性表 服务实现类
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-12
 */
@DubboService
public class CommoditySpecificationsAttributeServiceImpl extends ServiceImpl<CommoditySpecificationsAttributeMapper, CommoditySpecificationsAttribute> implements ICommoditySpecificationsAttributeService {
    @Autowired
    CommoditySpecificationsAttributeMapper commoditySpecificationsAttributeMapper;

    @Override
    public List<CommoditySpecificationsAttributeDTO> selectCommoditySpecificationsAttributeListByNameAndSId(Long commoditySpecificationsNameId, Long commoditySpecificationsId) {
        List<CommoditySpecificationsAttribute> commoditySpecificationsAttributeList = commoditySpecificationsAttributeMapper.findCommoditySpecificationsAttributeListByNameIdAndSId(commoditySpecificationsNameId, commoditySpecificationsId);
        List<CommoditySpecificationsAttributeDTO> commoditySpecificationsAttributeDTOList = new ArrayList<>();
        if(commoditySpecificationsAttributeList != null && commoditySpecificationsAttributeList.size() > 0){
            commoditySpecificationsAttributeList.stream().forEach(item -> {
                CommoditySpecificationsAttributeDTO commoditySpecificationsAttributeDTO = new CommoditySpecificationsAttributeDTO();
                BeanUtils.copyProperties(item, commoditySpecificationsAttributeDTO);
                commoditySpecificationsAttributeDTOList.add(commoditySpecificationsAttributeDTO);
            });
        }
        return commoditySpecificationsAttributeDTOList;
    }

    @Override
    public CommoditySpecificationsAttributeAddReq insertCommoditySpecificationsAttribute(CommoditySpecificationsAttributeAddReq obj, TokenUser tokenUser) {

        List<CommoditySpecificationsAttribute> commoditySpecificationsAttributeList = commoditySpecificationsAttributeMapper.findCommoditySpecificationsAttributeByNameAndSIdAndName(obj.getSpecificationsNameId(), obj.getSpecificationsId(), obj.getAttributeName());
        if(commoditySpecificationsAttributeList != null && commoditySpecificationsAttributeList.size() > 0){
            return null;
        }
        CommoditySpecificationsAttribute commoditySpecificationsAttribute = new CommoditySpecificationsAttribute();
        if(obj != null){
            BeanUtils.copyProperties(obj, commoditySpecificationsAttribute);
            commoditySpecificationsAttributeMapper.insert(commoditySpecificationsAttribute);
            obj.setId(commoditySpecificationsAttribute.getId());
        }
        return obj;
    }
}
